package org.luo.news.domain.article;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import org.luo.common.common.Result;
import org.luo.common.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {


    @Override
    public Result add(Article article) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        save(article);
        return Result.success(article);
    }

    @Override
    public Result<Page<Article>> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {


        Page<Article>  page = Page.of(pageNum, pageSize);
        Page<Article> data = lambdaQuery()
//                .eq(true, Article::getCreateUser, userId)
                .eq(categoryId != null, Article::getCategoryId, categoryId)
                .eq(state != null, Article::getState, state)
                .page(page);


        return Result.success(data);
    }

    @Override
    public Result<Article> detail(Integer id) {
        Article article = getById(id);

        return Result.success(article);
    }
}
